package com.example.shop.rabbitmq.comsumer;

import com.example.shop.goods.enums.EvaluationType;
import com.example.shop.mapper.EvaluationMapper;
import com.example.shop.mapper.OrderMapper;
import com.example.shop.order.enums.OrderState;
import com.example.shop.pojo.Evaluation;
import com.example.shop.pojo.Order;
import com.example.shop.rabbitmq.component.OrderComponents;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
public class EvaluateDeadOrderConsumer {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private EvaluationMapper evaluationMapper;

    /**
     * 待评价订单的死信队列的消费者
     * @param order 超过15天未评价的待评价的订单
     */
    @RabbitListener(queues = {OrderComponents.EVALUATE_DEAD_QUEUE},ackMode = "NONE")
    public void autoEvaluateOrder(Order order){
        Evaluation evaluation = new Evaluation();
        evaluation.setUserId(order.getUserId())
                .setOrderId(order.getOrderId())
                .setGoodsId(order.getOrderId())
                .setEvaluationContent("该用户觉得商品很好，给出了好评")
                .setEvaluationType(EvaluationType.FAVORABLE.getEvaluationTypeName())
                .setEvaluationDate(LocalDate.now());
        //对该待评价的订单进行自动评价，将评价数据插入到数据库中
        evaluationMapper.insert(evaluation);
        //更新订单的状态为已评价
        orderMapper.updateFieldById("order_state", OrderState.ALREADY_EVALUATE,order.getOrderId());
    }
}
